package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.FriendLinkTable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author system
 * @since 2024-10-23 22:16:35
 */
@Component
public class FriendLinkTableSpecifications {

    public static Specification<FriendLinkTable> queryList(FriendLinkTable friendLinkTable) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (friendLinkTable.getLinkId() != null) {
                predicates.add(builder.like(root.get("linkId" ).as(String.class), "%" + friendLinkTable.getLinkId() + "%" ));
            }
            if (friendLinkTable.getLinkName() != null) {
                predicates.add(builder.like(root.get("linkName" ).as(String.class), "%" + friendLinkTable.getLinkName() + "%" ));
            }
            if (friendLinkTable.getLinkUrl() != null) {
                predicates.add(builder.like(root.get("linkUrl" ).as(String.class), "%" + friendLinkTable.getLinkUrl() + "%" ));
            }
            if (friendLinkTable.getLinkDescription() != null) {
                predicates.add(builder.like(root.get("linkDescription" ).as(String.class), "%" + friendLinkTable.getLinkDescription() + "%" ));
            }
            if (friendLinkTable.getLogoPath() != null) {
                predicates.add(builder.like(root.get("logoPath" ).as(String.class), "%" + friendLinkTable.getLogoPath() + "%" ));
            }
            if (friendLinkTable.getSortOrder() != null) {
                predicates.add(builder.equal(root.get("sortOrder" ).as(Integer.class), friendLinkTable.getSortOrder()));
            }
            if (friendLinkTable.getStatus() != null) {
                predicates.add(builder.like(root.get("status" ).as(String.class), "%" + friendLinkTable.getStatus() + "%" ));
            }
            if (friendLinkTable.getVisitCount() != null) {
                predicates.add(builder.equal(root.get("visitCount" ).as(Integer.class), friendLinkTable.getVisitCount()));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}
